package fr.efrei.musicpliz.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.model.SelectItem;
import javax.persistence.Transient;

import fr.efrei.musicpliz.dto.CompteDto;
import fr.efrei.musicpliz.entity.Compte;
import fr.efrei.musicpliz.enums.CompteTypeEnum;
import fr.efrei.musicpliz.enums.NavigationOutcomeEnum;
import fr.efrei.musicpliz.service.CompteService;
import fr.efrei.musicpliz.web.util.FacesUtils;

@SuppressWarnings("serial")
public class SearchCompteController implements Serializable {

	private CompteService compteService;

	private CompteDto compte;
	
	private CompteDto destinataire;
	
	private CompteDto profile;
	
	private List<CompteDto> resultList;
	
	@Transient
	private transient HtmlDataTable dataTable;

	/**
	 * default empty constructor
	 */
	public SearchCompteController() {
	}

	/**
	 * Initialization method
	 */
	public void init() {
		compte = new CompteDto(null, null, null, null, 0, null, null, null, null,
				null, null, null, null, null, null, null, null, 0, 0, 0);
	}

	public String searchCompte() {
		resultList = compteService.searchComptesForDto(compte);
		return NavigationOutcomeEnum.SEARCH_RESULT.getPath();
	}
	
	public String getDetails() {
		Compte compte = (Compte) dataTable.getRowData();
		FacesUtils.addInfoMessage("you've requested some details for " + compte.getNom());
		return NavigationOutcomeEnum.HOME.getPath();
	}

	public String composeMessage() {
		destinataire = (CompteDto) dataTable.getRowData();
		return NavigationOutcomeEnum.COMPOSE.getPath();
	}
	
	public String viewProfile() {
		profile = (CompteDto) dataTable.getRowData();
		return NavigationOutcomeEnum.VIEWPROFILE.getPath();
	}
	
	public SelectItem[] getCompteTypes() {
		List<SelectItem> types = new ArrayList<SelectItem>();
		// Adding an emptyItem
		types.add(new SelectItem(null,""));
		for (CompteTypeEnum type : CompteTypeEnum.values()) {
			types.add(new SelectItem(type, type.getName()));
		}
		return types.toArray(new SelectItem[0]);
	}

	public void setCompteService(CompteService compteService) {
		this.compteService = compteService;
	}

	public CompteDto getDto() {
		return compte;
	}

	public void setDto(CompteDto dto) {
		compte = dto;
	}

	public List<CompteDto> getResultList() {
		return resultList;
	}

	public void setResultList(List<CompteDto> resultList) {
		this.resultList = resultList;
	}

	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable dataTable) {
		this.dataTable = dataTable;
	}

	public void setDestinataire(CompteDto destinaitaire) {
		this.destinataire = destinaitaire;
	}

	public CompteDto getDestinataire() {
		return destinataire;
	}
	
	public void setProfile(CompteDto profile) {
		this.profile = profile;
	}

	public CompteDto getProfile() {
		return profile;
	}
}
